package top.infopub.mgr.bpa.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.bpa.BpaProjectBudgetTypeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.jsonVo.BpaProjectBudgetJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.BpaProjectBudgetService;
import top.infopub.mgr.bpa.vo.BpaProjectBudget;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.model.bpa.ProjectBudget;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by wurz on 2021/6/19.
 */
@Controller
@RequestMapping("/projectBudget")
public class BpaProjectBudgetController extends GenericFlowController {

    @Autowired
    private BpaProjectBudgetService bpaProjectBudgetService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private OrgInfoService orgInfoService;
    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    /**
     * 跳转列表页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        /*预算类型*/
        List<Map<String, String>> budgetTypes = BpaProjectBudgetTypeEnum.asList();
        request.setAttribute("budgetTypes",budgetTypes);
        return "bpa/lay-projectBudget-list";
    }

    /**
     * 设备合同列表数据
     *
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<BpaProjectBudget> queryDataByCondition(BpaProjectBudget sureVo,
                                                        ParamPage paramPage,
                                                        String monthStartDateStr,
                                                        String monthEndDateStr) {
        Page<BpaProjectBudget> page = new Page<BpaProjectBudget>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = bpaProjectBudgetService.selectList(page,sureVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询  > 注意：方法命名必须为：toDealOrDetail
     *
     * @param id 投建项目id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id, String proCode) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id", id);
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");//登录人和审批人一致使用该字段是否显示审核信息
        if (StringUtils.isNotBlank(id)) {
            //修改成主表数据
            ProjectBudget proBudget = bpaProjectBudgetService.selectOne(id);
            if (proBudget != null){
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), proBudget.getProcessStatus())
                        || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), proBudget.getProcessStatus())) {
                    request.setAttribute("isAuditFlag", "0");
                    if (StringUtils.isNotBlank(proBudget.getProcessId()) && proBudget.getProcessId().length() > 10) {
                        request.setAttribute("processId", proBudget.getProcessId());
                        String auditTabFlag = bpaProcessManageService.checkShowAuditTab(proBudget.getProcessId(), vo);
                        if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), proBudget.getProcessStatus())
                                && StringUtils.equals("0", auditTabFlag)) {
                            request.setAttribute("isAuditFlag", "0");
                        }
                        request.setAttribute("auditTabFlag", auditTabFlag);
                    }
                }
            }
            request.setAttribute("proCode", proCode);
        }
        //统一一个详情，根据主表返回的类型显示不同的数据--前端会处理，后端只要返回标识
        return "bpa/lay-ProjectPreSettlement-detail";
    }

    @RequestMapping("/queryDetail")
    @ResponseBody
    public RspVo queryDetail(String id) {
        return RspVo.getSuccessResponse(bpaProjectBudgetService.queryDetailById(id));
    }

    /**
     * 根据项目编码查询项目基础信息
     * @param proCode
     * @return
     */
    @RequestMapping("/queryProInfoByProCode")
    @ResponseBody
    public RspVo queryProInfoByProCode(String proCode) {
        return RspVo.getSuccessResponse(bpaProjectBudgetService.queryProInfoByProCode(proCode));
    }

    /**
     * 新增/编辑
     * @param bodyStr
     * @param submitType
     * @return
     */
    @RequestMapping("/edit")
    public @ResponseBody RspVo edit(String bodyStr, Integer submitType) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String str = StringEscapeUtils.unescapeJava(bodyStr);
        BpaProjectBudgetJsonVo jsonVo = JSON.parseObject(str, BpaProjectBudgetJsonVo.class);

        //传入参数自己看情况
        RspVo rspVo = bpaProjectBudgetService.edit(jsonVo, submitType, vo);
        return rspVo;
    }
}
